<%@page contentType="text/html;charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>
<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title><spring:message code="html.title"/></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="<c:url value="/static/styles/style.css" />" type="text/css"/>
    <%--<link type="text/css" href="<c:url value="/static/styles/overcast/jquery-ui-1.8.17.custom.css" />"
              rel="Stylesheet"/>--%>
    <link rel="stylesheet" href="<c:url value="/static/styles/jquery.dataTables.css" />" type="text/css"/>
    <script type="text/javascript" src="<c:url value="/static/js/jquery-1.7.1.min.js" />"></script>
    <script type="text/javascript" src="<c:url value="/static/js/jquery.dataTables-1.9.2.min.js" />"></script>

    <script type="text/javascript">
        function show_confirm(userName) {
            return confirm("<spring:message code="msg.users.delete"/>" + " " + userName + "?");
        }
    </script>
</head>
<body>
<div id="locationPage">
    <%-- Menu --%>
    <c:import url="../include/menu.jsp">
        <c:param name="page" value="users"/>
    </c:import>

    <%-- Locations list --%>
    <h2><spring:message code="header.users.view"/></h2>

    <div id="table">
        <table id="users_table">
            <thead>
            <tr>
                <th style="width:3%">#</th>
                <th style="width:15%"><spring:message code="table.users.name"/></th>
                <th style="width:10%"><spring:message code="table.users.created"/></th>
                <th style="width:10%"><spring:message code="table.users.expires"/></th>
                <th style="width:10%"><spring:message code="table.users.licenseName"/></th>
                <th style="width:15%"><spring:message code="table.users.licenseCode"/></th>
                <th style="width:10%"><spring:message code="table.users.mac"/></th>
                <th style="width:12%"><spring:message code="table.users.mac.enabled"/></th>
                <th style="width:15%"><spring:message code="table.users.actions"/></th>
            </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    <p><input type="button" value="<spring:message code="action.users.add"/>"
              onclick="document.location.href='<c:url value="/users/add"/>'">
    </p>
</div>
</body>
<script type="text/javascript">
    function getURLParameter(name) {
        var res = decodeURI(
                (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
        );
        if (!res || res=="null"){
            return "en";
        } else {
            return res;
        }
    }


    $(document).ready(function () {
        $("#users_table").dataTable({
            "oLanguage": {
                "sUrl": "<c:url value="/static/datatables/"/>" + getURLParameter('lang') + ".txt"
            },
            "bProcessing": true,
            "bServerSide": true,
            "sPaginationType": "full_numbers",
            "bFilter": false,
//            "bJQueryUI":true,
            "sAjaxSource": "<c:url value="/users/getData"/>",
            "aoColumnDefs": [
                { "mDataProp": "id", "bSortable": true, "sType": "numeric", "aTargets": [0] },
                { "mDataProp": "userName", "bSortable": true, "aTargets": [1] },
                { "mDataProp": "createDate", "bSortable": true, "sType": "date", "aTargets": [2] },
                { "mDataProp": "expireDate", "bSortable": true, "sType": "date", "aTargets": [3] },
                { "mDataProp": "licenseName", "bSortable": true, "aTargets": [4] },
                { "mDataProp": "licenseCode", "bSortable": false, "aTargets": [5] },
                { "mDataProp": "mac", "bSortable": false, "aTargets": [6],
                    "fnRender": function (oObj) {
                        var mac = oObj.aData["mac"];
                        if (mac != null) {
                            return mac;
                        } else {
                            return "<spring:message code="marker.mac.not.set"/>";
                        }
                    }
                },
                { "mDataProp": "macChecking", "bSortable": true, "aTargets": [7] },
                { "mDataProp": null, "bSortable": false, "bSearchable": false, "aTargets": [8],
                    "fnRender": function (oObj) {
                        var id = oObj.aData["id"];
                        var userName = oObj.aData["userName"];
                        <c:url value='/users/resetMac' var="resetMacUrl"/>
                        <c:url value='/users/edit/' var="editUrl"/>
                        <c:url value='/users/deleteUser' var="deleteUrl"/>
                        var deleteHref = "<a onclick='return show_confirm (\"" + userName + "\");' href='${deleteUrl}?id=" + id + "'><spring:message code='actions.users.delete'/></a>";
                        var result = "<a href='${resetMacUrl}?id=" + id + "'><spring:message
                        code='actions.users.reset.mac'/></a>&nbsp;&nbsp;<a href='${editUrl}" + id +
                                "'/><spring:message code='actions.users.edit'/></a>&nbsp;&nbsp;" + deleteHref;
                        return result;
                    }
                }
            ],
            "aaSorting": [
                [ 0, "asc" ]
            ]
        });
    });
</script>
</html>